package com.yantu.controller.student;

import com.yantu.bean.*;
import com.yantu.mapper.*;
import com.yantu.util.Result;
import com.yantu.view.student.StudentAnswerItemView;
import com.yantu.view.student.StudentAnswerSubmitView;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@ApiModel("试卷提交接口信息")
public class StudentAnswerSubmitController {

    @Autowired
    QuestionMapper questionMapper;

    @Autowired
    ExamPaperAnswerMapper examPaperAnswerMapper;

    @Autowired
    ExamPaperQuestionCustomerAnswerMapper examPaperQuestionCustomerAnswerMapper;

    @Autowired
    TextContentMapper textContentMapper;

    @Autowired
    ExamPaperMapper examPaperMapper;
//
//    @ModelAttribute("AnswerSubmitView")

    @PostMapping("/answerSubmit/")
    @ApiOperation("试卷提交")
    public Result answerSubmit(@RequestBody StudentAnswerSubmitView studentAnswerSubmitView)  {
        ExamPaper examPaper = examPaperMapper.selectById(studentAnswerSubmitView.getId());
        //创建答题卡
        ExamPaperAnswer examPaperAnswer = new ExamPaperAnswer();
        examPaperAnswer.setExamPaperId(examPaper.getId());
        examPaperAnswer.setPaperName(examPaper.getName());
        examPaperAnswer.setPaperType(examPaper.getPaperType());
        examPaperAnswer.setSubjectId(examPaper.getSubjectId());
        examPaperAnswer.setPaperScore(examPaper.getScore());
        examPaperAnswer.setQuestionCount(examPaper.getQuestionCount());
        examPaperAnswer.setDoTime(studentAnswerSubmitView.getDoTime());
        examPaperAnswer.setStatus(1);


        java.util.Date  date=new java.util.Date();
        java.sql.Date  sqldate=new java.sql.Date(date.getTime());
        examPaperAnswer.setCreateTime(sqldate);
        examPaperAnswer.setCreateUser(studentAnswerSubmitView.getUId());

        examPaperAnswerMapper.insert(examPaperAnswer);

        //创建小题答题卡
        Integer score = 0;//记录系统判分
        Integer questionCorrent = 0;//记录答题正确个数
        List<StudentAnswerItemView> answerItemViews = studentAnswerSubmitView.getAnswerItems();
        for (StudentAnswerItemView answerItemView : answerItemViews) {
            System.out.println("+++++++++++++++++++++++++++++++++====");

            ExamPaperQuestionCustomerAnswer examPaperQuestionCustomerAnswer = new ExamPaperQuestionCustomerAnswer();
            examPaperQuestionCustomerAnswer.setQuestionId(answerItemView.getQuestionId());
            examPaperQuestionCustomerAnswer.setExamPaperAnswerId(studentAnswerSubmitView.getId());
            examPaperQuestionCustomerAnswer.setExamPaperAnswerId(examPaperAnswer.getId());
            Question question = questionMapper.selectById(answerItemView.getQuestionId());
            examPaperQuestionCustomerAnswer.setQuestionType(question.getQuestionType());
            examPaperQuestionCustomerAnswer.setSubjectId(question.getSubjectId());
            examPaperQuestionCustomerAnswer.setQuestionScore(question.getScore());
            examPaperQuestionCustomerAnswer.setQuestionTextContentId(question.getInfoTextContentId());
            examPaperQuestionCustomerAnswer.setExamPaperId(studentAnswerSubmitView.getId());


            //系统判题  单选题 多选题 判断题
            if(question.getQuestionType()==1||question.getQuestionType()==2||question.getQuestionType()==3){

                examPaperQuestionCustomerAnswer.setAnswer(answerItemView.getContent());

                //判断是否与答案相等
                if(answerItemView.getContent().equals(question.getCorrect())){
                    score = score + question.getScore();
                    questionCorrent = questionCorrent + 1;
                    examPaperQuestionCustomerAnswer.setCustomerScore(question.getScore());
                    examPaperQuestionCustomerAnswer.setDoRight(1);
                }
                else {
                    examPaperQuestionCustomerAnswer.setCustomerScore(0);
                    examPaperQuestionCustomerAnswer.setDoRight(0);
                }
            }
            //填空题 简答题
            else{
                examPaperQuestionCustomerAnswer.setCustomerScore(0);
                examPaperQuestionCustomerAnswer.setCustomerScore(0);

                TextContent textContent = new TextContent();
                textContent.setCreateTime(sqldate);

                //填空题
                if(question.getQuestionType()==4){

                }

                //简答题
                else  {
                    if(answerItemView.getContent()!=null){
                        textContent.setContent(answerItemView.getContent());
                        textContentMapper.insert(textContent);
                    }

                }

                if(textContent.getId()!=null){
                    examPaperQuestionCustomerAnswer.setTextContentId(textContent.getId());

                }
            }

            examPaperQuestionCustomerAnswer.setCreateUser(studentAnswerSubmitView.getUId());
            examPaperQuestionCustomerAnswer.setCreateTime(sqldate);
            examPaperQuestionCustomerAnswer.setItemOrder(answerItemView.getItemOrder());
            examPaperQuestionCustomerAnswerMapper.insert(examPaperQuestionCustomerAnswer);


        }

        examPaperAnswer.setQuestionCorrect(questionCorrent);
        examPaperAnswer.setSystemScore(score);
        examPaperAnswer.setUserScore(score);
        examPaperAnswerMapper.updateById(examPaperAnswer);

        return Result.succ(200, "提交成功", score);


    }

}
